﻿using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Text;

namespace ReaWTM.LayuiHelper
{
	[HtmlTargetElement("wt:dialog", TagStructure = TagStructure.Unspecified)]
	public class DialogTagHelper : BaseTag
	{
		private string id;
		public string Id
		{
			get
			{
				if (string.IsNullOrEmpty(id))
				{
					id = $"{this.GetType().Name.Replace("TagHelper", "")}{Guid.NewGuid():N}";
				}
				return id;
			}
			set { id = value; }
		}
		/// <summary>
		/// 设置高度
		/// </summary>
		public int Height { get; set; }

		/// <summary>
		/// 设置高度
		/// </summary>

		public bool IsPercent { get; set; }

		/// <summary>
		/// 设置为百分比数据
		/// </summary>
		/// <summary>
		/// 响应式弹窗
		/// 必须同时设置高度和宽度，否则无法计算响应尺寸
		/// </summary>
		public bool IsResponsive { get; set; } = true;

		/// <summary>
		/// 弹窗的标题
		/// </summary>
		public string Title { get; set; }

		/// <summary>
		/// 宽度
		/// </summary>

		public int Width { get; set; }

		public override void Process(TagHelperContext context, TagHelperOutput output)
		{
			SetCascadeValue(context, this);

			StringBuilder builder = new StringBuilder();

			output.TagName = "div";
			output.TagMode = TagMode.StartTagAndEndTag;
			output.Attributes.Add("id", Id);
			output.Attributes.Add("class", $"wtm-dialog{GetBgColorClass()}{GetFontColorClass()}{GetFontSizeClass()}");


			builder.Append($@"
<script>
wtm.dialog_Render({{
id:'{Id}',
width:{Width},
height:{Height},
title:'{Title}',
isResponsive:{IsResponsive.ToString().ToLower()},
isPercent:{IsPercent.ToString().ToLower()},
}});
</script>
");
			output.PostElement.SetHtmlContent(builder.ToString());

		}
	}
}